package com.lagou.user.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lagou.entity.User;
import com.lagou.entity.UserDTO;
import com.lagou.user.UserService;
import com.lagou.util.FileAction;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartHttpServletRequest;

/**
 * 用户controller层
 *
 * @author huangshuxian
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Reference//远程消费
    private UserService userService;

    @GetMapping("login")
    public UserDTO login(String phone, String password,
                         String nickname, String heading){
        UserDTO dto = new UserDTO();
        User user = null;

        //检测手机号是否注册过
        Integer i = userService.checkPhone(phone);
        if (i == 0){
            //自动注册并登录
            userService.register(phone, password, nickname, heading);
            dto.setMessage("手机号尚未注册，系统已帮您自动注册，请牢记密码！");
            user = userService.login(phone, password);
        }else{
            user = userService.login(phone, password);
            if (user == null){
                dto.setState(300);
                dto.setMessage("帐号密码不匹配，登录失败！");
            }else{
                dto.setState(200);
                dto.setMessage("登录成功！");
            }
        }

        dto.setContent(user);
        return dto;
    }


    @RequestMapping("updateUserInfo")
    public UserDTO updateUserInfo(){
//        @RequestBody User user
        User user = new User();
        user.setId(100030024);
        //本地图片路径
        //todo
        String filePath = "E:\\证件照\\IMG_0608.JPG";
        //使用fastFDS上传到服务器，获取id
        String fname = FileAction.upload(filePath);
        user.setPortrait(fname);
        //todo
        user.setName("小仙女");
        Integer i = userService.updateUserInfo(user);
        UserDTO dto = new UserDTO();
        if (i > 0){
            dto.setState(200);
            dto.setMessage("设置成功！");
        }else{
            dto.setState(300);
            dto.setMessage("设置失败！");
        }
        return dto;
    }

    @RequestMapping("updatePassword")
    public UserDTO updatePassword(){
        User user = new User();
        user.setId(100030024);
        user.setPassword("123456");

        Integer i = userService.updatePassword(user);
        UserDTO dto = new UserDTO();
        if (i > 0){
            dto.setState(200);
            dto.setMessage("修改成功！");
        }else{
            dto.setState(300);
            dto.setMessage("修改失败！");
        }
        return dto;
    }
}
